/**
 * Program  : FireActivity.java
 * Author   : long
 * Create   : 2016年3月30日 上午11:32:41
 *
 * Copyright 2016 by Embedded Internet Solutions Inc.,
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Embedded Internet Solutions Inc.("Confidential Information").  
 * You shall not disclose such Confidential Information and shall 
 * use it only in accordance with the terms of the license agreement 
 * you entered into with Embedded Internet Solutions Inc.
 *
 */
	
package cn.broadin.app.hotel;

import java.io.File;
import java.io.IOException;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import cn.broadin.app.hotel.app.AppConfig;
import cn.broadin.app.hotel.util.XLog;
import cn.broadin.app.hotel.util.XToast;
import cn.broadin.app.hotel.util.XUtils;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;
/**
 * 火灾紧急相应Activity
 * 
 * @author   long
 * @version  1.0.0
 * @date 2016年3月30日上午11:32:41
 */
public class FireActivity extends BaseActivity{
	
	 private MediaPlayer mMediaPlayer;
	 public static Activity mActivity;
	 
	 private GifImageView mImageView;
	 
	 public static final String MAP_KEY = "fire_map_key";
	 
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fire);
		
		mActivity = this;
		
		mImageView = (GifImageView) this.findViewById(R.id.map_fire);
	}
	
	private void initMediaPlayer(){
		XLog.d("initMediaPlayer...");
    	try {
    		mMediaPlayer = new MediaPlayer();
    		mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    		mMediaPlayer.setDataSource(AppConfig.FIRE_AUDIO_PATH);
    		mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					XLog.e("setOnErrorListener!!! what: " + what + ", extra: " + extra);
					return true;
				}
			});
			mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
				@Override
				public void onPrepared(MediaPlayer mp) {
					mMediaPlayer.start();
				}
			});
			mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
				@Override
				public void onCompletion(MediaPlayer mp) {
					XLog.d("setOnCompletionListener!!! ");
					try {
						mMediaPlayer.stop();
						mMediaPlayer.reset();
						mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
						mMediaPlayer.setDataSource(AppConfig.FIRE_AUDIO_PATH);
						mMediaPlayer.prepareAsync();
			        } catch(Exception e) {
			        	XLog.e("setOnCompletionListener!!! " + e.getMessage());
			        }
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
			XLog.d("======================================================");
		}
	}
	
	/**
	 * @see android.app.Activity#onPause()
	 * @exception
	 * @author   long
	 * @since 1.0.0
	 * @date 2016年3月30日 上午11:56:56
	*/
	@Override
	protected void onPause() {
		if(mMediaPlayer != null &&  mMediaPlayer.isPlaying()){
			mMediaPlayer.release();
		}
		mMediaPlayer = null;
		super.onPause();
	}
	
	/**
	 * @see android.app.Activity#onResume()
	 * @exception
	 * @author   long
	 * @since 1.0.0
	 * @date 2016年3月30日 上午11:57:14
	*/
	@Override
	protected void onResume() {
		try {
			// 播放MP3
			initMediaPlayer();
			
			String url = XUtils.getSharedPreferenceConfig(this, MAP_KEY);
			if(!TextUtils.isEmpty(url)){
				GifDrawable gifFrom = new GifDrawable(new File(url));
				mImageView.setImageDrawable(gifFrom);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		super.onResume();
	}
	
	/**
	 * @see android.app.Activity#onDestroy()
	 * @exception
	 * @author   long
	 * @since 1.0.0
	 * @date 2016年3月30日 上午11:56:41
	*/
	@Override
	protected void onDestroy() {
		
		if(mMediaPlayer != null &&  mMediaPlayer.isPlaying()){
			mMediaPlayer.release();
		}
		mMediaPlayer = null;
		
		mActivity = null;
		
		super.onDestroy();
	}
	
	public static void fireOver(){
		if(FireActivity.mActivity != null){
			mActivity.finish();
		}
	}
	
	/**
	 * 加载bitmap图片
	 * 
	 * @author   long
	 * @param imageView
	 * @return void
	 * @exception
	 * @author   long
	 * @since 1.0.0
	 * @date 2015年9月23日 下午4:32:44
	 */
	public void loadBitmap(final ImageView imageView, String uri){
		BitmapUtils bitmapUtils = new BitmapUtils(this);
		BitmapDisplayConfig displayConfig = new BitmapDisplayConfig();
		bitmapUtils.configDefaultDisplayConfig(displayConfig);
		bitmapUtils.display(imageView, uri);
	}
	
	public boolean dispatchKeyEvent(KeyEvent event){
		int keyCode = event.getKeyCode();
		switch(keyCode){
		case KeyEvent.KEYCODE_BACK:
		case KeyEvent.KEYCODE_MENU:
		case KeyEvent.KEYCODE_HOME:
			return true;
		}
		return super.dispatchKeyEvent(event);
		
	}
}
